package com.yuju.order.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.exception.BusinessException;
import com.yuju.order.dao.IInvoiceDao;
import com.yuju.order.dao.entity.Invoice;
import com.yuju.order.enums.InvoiceReturnCodeEnum;
import com.yuju.order.enums.InvoiceTypeEnum;
import com.yuju.order.model.InvoiceModel;
import com.yuju.order.service.IInvoiceService;
import com.yuju.order.service.IReserveRoomService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：InvoiceServiceImpl  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:41  <br />
 * @version : 1.1 <br />
 */
@Service
public class InvoiceServiceImpl extends AbstractBaseService<InvoiceModel, Invoice,String> implements IInvoiceService{
	@Autowired
	private IInvoiceDao invoiceDao;
	
	@Override
	public IBaseDao<InvoiceModel, Invoice, String> getDao() {
		// TODO Auto-generated method stub
		return invoiceDao;
	}
	@Override
	public int save(InvoiceModel model) {
		// 保存数据
		model.setId(Utils.getUUID());
		model.setGmtCreate(new Date());
		model.setGmtModified(new Date());
		model.setInvoiceTypeName(InvoiceTypeEnum.getNameByValue(model.getInvoiceType()));
		int rows = super.save(model);
		return rows;
	}
	@Override
	public int updateSelective(InvoiceModel model) {
		// 保存数据
		model.setGmtModified(new Date());
		model.setInvoiceTypeName(InvoiceTypeEnum.getNameByValue(model.getInvoiceType()));
		int rows = super.updateSelective(model);
		return rows;
	}
	@Override
	public int updateInvoice(InvoiceModel model) {
		/**
		 * 更新时限制
		 * 开票时间48小时内克修改，超出不可修改
		 */
		InvoiceModel modelFind = super.findById(model.getId());
		if(modelFind == null){
			throw new BusinessException(InvoiceReturnCodeEnum.INVOICE_QUERY_FAILURE.getCode()
					, InvoiceReturnCodeEnum.INVOICE_QUERY_FAILURE.getDesc());
		}
		int hours =(int) (modelFind.getGmtCreate().getTime()- new Date().getTime())/(1000*3600);
		if(hours > 48){
			throw new BusinessException(InvoiceReturnCodeEnum.INVOICE_UPDATE_OUT_FAILURE.getCode()
					, InvoiceReturnCodeEnum.INVOICE_UPDATE_OUT_FAILURE.getDesc());
		}
		return updateSelective(model);
	}
	

}
